package com.edu.hre.struts.action.pim;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpContactDetailUpdateForm;
import com.google.gwt.http.client.Response;

public class EmpContactDetailUpdateAction extends Action {

	private static final Logger logger = Logger.getLogger(EmpContactDetailUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "empContactDetailUpdate";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		
		String idStr = request.getParameter("id");

		HreUser tmpUser = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		if (!EmployeeListAction.isAllowedToViewEmployeeList(tmpUser, CommonUtils.parseInt(idStr, -1))) {
			response.sendError(Response.SC_BAD_REQUEST);
			return null;
		}
		
		// Get the authorization of the current user for the specified operations. 
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);
		
		EmpContactDetailUpdateForm beanForm = (EmpContactDetailUpdateForm) form;
		beanForm.setUpdatable(userRoleScreen.isCanUpdate());
		beanForm.setIsAddNew(false);
		
		if(request.getParameter("btnUpdate") == null){

			int id = CommonUtils.getIntParameter("id", request);
			
			EmployeeListAction.setAttributesForRequest(id, request);
			
			HreEmployee tmp = baseService.getFirstPojo("empNumber", Compare.EQUAL, id, null, null, 0, HreEmployee.class);
			beanForm.setEmpNumber(tmp.getEmpNumber());
			beanForm.setAddressStreet1(CommonUtils.normalizeString(tmp.getEmpStreet1()));
			beanForm.setAddressStreet2(CommonUtils.normalizeString(tmp.getEmpStreet2()));
			beanForm.setDistrictCode(CommonUtils.normalizeString(tmp.getDistrictCode()));
			beanForm.setProvinceCode(CommonUtils.normalizeString(tmp.getProvinCode()));
			beanForm.setZipCode(CommonUtils.normalizeString(tmp.getEmpZipcode()));
			beanForm.setHomePhone(CommonUtils.normalizeString(tmp.getEmpHmTelephone()));
			beanForm.setMobilePhone(CommonUtils.normalizeString(tmp.getEmpMobile()));
			beanForm.setWorkPhone(CommonUtils.normalizeString(tmp.getEmpWorkTelephone()));
			beanForm.setWorkEmail(CommonUtils.normalizeString(tmp.getEmpWorkEmail()));
			beanForm.setOtherEmail(CommonUtils.normalizeString(tmp.getEmpOthEmail()));

			initFormBean(beanForm);
			beanForm.setMessage(StringConstants.BLANK);
			beanForm.setSuccess(false);
			return mapping.findForward(ACTION_NAME);
		}
		
		EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			beanForm.setSuccess(false);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		HreEmployee tmp = baseService.getFirstPojo("empNumber", Compare.EQUAL, beanForm.getEmpNumber(), null, null, 0, HreEmployee.class);
		tmp.setEmpStreet1(CommonUtils.normalizeString(beanForm.getAddressStreet1()));
		tmp.setEmpStreet2(CommonUtils.normalizeString(beanForm.getAddressStreet2()));
		tmp.setDistrictCode(CommonUtils.normalizeString(beanForm.getDistrictCode()));
		tmp.setProvinCode(CommonUtils.normalizeString(beanForm.getProvinceCode()));
		tmp.setEmpZipcode(CommonUtils.normalizeString(beanForm.getZipCode()));
		tmp.setEmpHmTelephone(CommonUtils.normalizeString(beanForm.getHomePhone()));
		tmp.setEmpMobile(CommonUtils.normalizeString(beanForm.getMobilePhone()));
		tmp.setEmpWorkTelephone(CommonUtils.normalizeString(beanForm.getWorkPhone()));
		tmp.setEmpWorkEmail(CommonUtils.normalizeString(beanForm.getWorkEmail().toLowerCase()));
		tmp.setEmpOthEmail(CommonUtils.normalizeString(beanForm.getOtherEmail().toLowerCase()));
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			beanForm.setSuccess(false);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		beanForm.setMessage("Contact details have been updated successfully!");
		beanForm.setSuccess(true);
		return mapping.findForward(ACTION_NAME);
	}
	
	private void initFormBean(EmpContactDetailUpdateForm beanForm) {

		beanForm.setAllDistricts(ComboBoxInit.getAllDistricts(true));
		beanForm.setAllProvinces(ComboBoxInit.getAllProvinces(true));
	}
	
	private String validateForm (EmpContactDetailUpdateForm beanForm) {
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getWorkEmail())) && !CommonUtils.verifyEmail(CommonUtils.normalizeString(beanForm.getWorkEmail()))){
			return "Work email is not valid!";
		}
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getOtherEmail())) && !CommonUtils.verifyEmail(CommonUtils.normalizeString(beanForm.getOtherEmail()))){
			return "Other email is not valid!";
		}

		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getHomePhone())) && !CommonUtils.verifyPhoneNumber(CommonUtils.normalizeString(beanForm.getHomePhone()))){
			return "Home phone is not valid!";
		}
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getMobilePhone())) && !CommonUtils.verifyPhoneNumber(CommonUtils.normalizeString(beanForm.getMobilePhone()))){
			return "Mobile phone is not valid!";
		}
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getWorkPhone())) && !CommonUtils.verifyPhoneNumber(CommonUtils.normalizeString(beanForm.getWorkPhone()))){
			return "Work phone is not valid!";
		}

		return StringConstants.BLANK;
	}
}
